#include "sys_stdio.h"
#include "task_manager.h"
#include "systemframebuffer.h"
#include "gui_object.h"
#include "gui_task.h"
#include "lcd.h"
#include "main_task.h"

struct gs_gui_task
{
    struct lcd_framebuffer lcd_data;
    struct lcd_display_tools lcd_tools;
    struct sj_task_t gui_task_handle;
    struct gui_console gui_task_console;
    sj_int8 fd_lcd;
};
static int fd_iwdg;
int fd_adc;
static struct gs_gui_task gs_gui_task_all = {0};

static TASK_MODULE_DATA(gui,GUI_TASK_PRIO,GUI_STK_SIZE);

static void lcd_display(sj_int8 x,sj_int8 y,sj_char *content,sj_int8 mode)
{
    if(content == NULL)
    {
        return ;
    }
    sj_lcd_print(x, y, content, mode, gs_gui_task_all.lcd_data.buffer);
/*
    else if(lcd_show_mode == 2)
    {
        sj_kprintf("[slj] enter show date \r\n");
        sj_lcd_show_date((const unsigned char *)content,y,x, data.buffer);
    }
    else if(lcd_show_mode == 3)
    {
        sj_lcd_show_pic((const uint8_t*)&g_Week[0],y,x,32,0, data.buffer);
        sj_lcd_show_pic((const uint8_t*)&g_Week[2],y,102,16,0, data.buffer);
    }
*/
}

static void lcd_pic_display(sj_int8 x,sj_int8 y,struct desktop_signal_value *val)
{
    sj_show_desktop_pic(x, y, val, gs_gui_task_all.lcd_data.buffer);
}

void gui_task_run_now(struct gui_console *console)
{
    gs_gui_task_all.gui_task_console =*console;
    sj_task_run_onetime(&gs_gui_task_all.gui_task_handle);
}

void gui_task_init()
{
    gui_entry_get(&gs_gui_task_all.gui_task_console);

    fd_iwdg = sj_open("/dev/iwdg", 0);
    fd_adc = sj_open("/dev/adc1", 0);
    gs_gui_task_all.fd_lcd = sj_open("/dev/fb", 0);
    gs_gui_task_all.lcd_tools.display = lcd_display;
    gs_gui_task_all.lcd_tools.display_pic = lcd_pic_display;
    sj_ioctl(gs_gui_task_all.fd_lcd, LCD_GET_BUF_INFO, &gs_gui_task_all.lcd_data);
    lcd_show_content(&gs_gui_task_all.gui_task_console, &gs_gui_task_all.lcd_tools);
    sj_flush(gs_gui_task_all.fd_lcd);

    gs_gui_task_all.gui_task_handle.name="gui";
    gs_gui_task_all.gui_task_handle.run_func = gui_task;
    GET_TASK_MODULE_DATA(gs_gui_task_all.gui_task_handle.task_data,gui);
    gs_gui_task_all.gui_task_handle.task_data->periodtime=1000;
    sj_task_create(&gs_gui_task_all.gui_task_handle);

}

void gui_task(void *arg)
{
    sj_ioctl(gs_gui_task_all.fd_lcd, LCD_CLEAN_BUFFER, &gs_gui_task_all.lcd_data);
    int ret = lcd_show_content(&gs_gui_task_all.gui_task_console, &gs_gui_task_all.lcd_tools);
    sj_flush(gs_gui_task_all.fd_lcd);
    sj_write(fd_iwdg, NULL, 0);
    sj_read(fd_adc, NULL, 0);
}
